package com.at.combine9;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/9/25 18:17
 */
public class ConnectDemo2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

//        DataStreamSource<Integer> source1 = env.fromElements(1, 2, 3, 4, 5);
//        DataStreamSource<String> source2 = env.fromElements("a", "b", "c");

        SingleOutputStreamOperator<Integer> source1 = env.socketTextStream("app01", 7777).map(x -> Integer.parseInt(x));
        DataStreamSource<String> source2 = env.socketTextStream("localhost", 8888);

        ConnectedStreams<Integer, String> connect = source1.connect(source2);

        /**
         * TODO 使用connect合流
         * 1、一次只能连接2条流
         * 2、流的数据类型可以不一样
         * 3、连接后可以调用map、flatmap、process来处理，但是各处理各的
         */
        SingleOutputStreamOperator<String> rst = connect.map(new CoMapFunction<Integer, String, String>() {
            private static final long serialVersionUID = 6987631006575762667L;

            @Override
            public String map1(Integer value) throws Exception {
                return "来源于数字流：" + value;
            }

            @Override
            public String map2(String value) throws Exception {
                return "来源于字母流" + value;
            }
        });

        rst.print();

        env.execute();
    }
}
